package s;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Statement;

import cbir.Img;
import cbir.Rss;
import cbir.Search;

public class QBC extends HttpServlet{
	
		private static final long serialVersionUID = 1L;
	
		private Properties props;
		private String propsPath;
		private static final int numResults = 25;

		@Override
		public void init(ServletConfig config) throws ServletException {
			super.init(config);
			
			this.propsPath = this.getServletContext().getRealPath("/");
			this.props = new Properties();
			try {
				props.loadFromXML(new FileInputStream(propsPath + "properties.properties"));
			} catch (FileNotFoundException e) {e.printStackTrace();} 
			catch (IOException e) {e.printStackTrace();}
	}

		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse res)	throws ServletException, IOException {

			Color c = new Color( Integer.parseInt( req.getParameter("color") ) );

				Search s = new Search(this.getServletContext().getRealPath("/") + props.getProperty("IMG_PATH"), this.getServletContext().getRealPath("/") + props.getProperty("INDEX_PATH"));
				ArrayList<Img> lcr = s.searchForColor(numResults, c, req.getServerName(), req.getContextPath());

				Connection connection = null;
				Statement statement;
				boolean found = false;
				
				try {
					Class.forName("com.mysql.jdbc.Driver");
					connection = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USER"), props.getProperty("DB_PASS"));
					statement = (Statement) connection.createStatement();
					
					// Query
					String query = "SELECT Filename, Height, Width FROM img WHERE";
					for(int j = 0; j < lcr.size()-1; j++){
						query = query.concat(" Filename = '" + lcr.get(j).getName() + "' OR");
					}
					query = query.concat(" Filename = '" + lcr.get(lcr.size()-1).getName() + "';" );
					
					ResultSet rs = statement.executeQuery(query);

					found = false;

					while(rs.next()){
						String name = rs.getString("Filename");	
						
						ListIterator<Img> lcc = lcr.listIterator();
						found = false;
						
						while(lcc.hasNext() && found == false){
								Img cc = lcc.next();
								if(cc.getName().compareTo(name) == 0){	// name è una thumbname in realtà
									cc.setHeight(rs.getInt("Height"));
									cc.setWidth(rs.getInt("Width"));
									found = true;
								}
						}
						
					}
				
				}catch (SQLException e1) {e1.printStackTrace();} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
				
				if(lcr.size() == 0)
					return;

				Rss r = new Rss(getServletName(), req.getServerName(), req.getContextPath());
				r.createRss(lcr);
				
				PrintWriter pw = res.getWriter();
				pw.write(r.getRss());
		}
}